<?php

namespace admin\modules\system\models\searches;

use Yii;
use common\components\wajaxcrud\generators\crud\SpecialFilterTrait;
use common\components\wtools\tools\Model;
use yii\data\ActiveDataProvider;
use yii\db\ActiveQuery;
use yii\db\Expression;
use common\components\wtools\tools\ArrayHelper;
use admin\models\db\Banners as BannersModel;

/**
 * Banners represents the model behind the search form about `admin\models\db\Banners`.
 */
class Banners extends BannersModel
{
    use SpecialFilterTrait;

    const SCENARIO_EDITABLE = 'editable';

    public function rules()
    {
        return [
            [['id', 'status', 'url', 'banner_sort', 'title'], 'safe'],
            [['created_at', 'updated_at'], 'match', 'pattern' => '/^.+\s\-\s.+$/'],
        ];
    }

    public function scenarios()
    {
        return ArrayHelper::merge(parent::scenarios(), [
            self::SCENARIO_EDITABLE => ['banner_sort','title'],
        ]);
    }

    /**
     * Creates data provider instance with search query applied
     * @param array $params
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = self::find();
        $this->load($params);
        $this->fieldFilter($query, 'id');
        $this->rangeFilter($query, 'created_at', true);
        $this->rangeFilter($query, 'updated_at', true);
        $this->fieldFilter($query, 'status');
        $this->fieldFilter($query, 'banner_sort');
        $this->fieldFilter($query, 'url', 'like');

        $this->fieldFilter($query, 'title', 'like');

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => ['defaultOrder' => ['id' => SORT_DESC, ]],
            ]);
        if (!$this->validate()) {
            return $dataProvider;
        }
        return $dataProvider;
    }
}
